import Vue from "vue";
import VueRouter from "vue-router";
import NProgress from "nprogress";
import "nprogress/nprogress.css";
import VueCookies from "vue-cookies";

NProgress.configure({ showSpinner: false });

Vue.use(VueRouter);

let routes = [
    {
        path: "/",
        redirect: "index",
    },
    {
        path: "/login",
        name: "login",
        component: () => import("@/views/login/login.vue"),
        meta: {
            title: "登录",
            requireAuth: false,
            tabHidden: true,
        },
    },
    {
        path: "/index",
        name: "index",
        component: () => import("@/views/index.vue"),
        meta: {
            title: "首页",
            requireAuth: true,
            tabHidden: false,
        },
    },
    {
        path: "/entry",
        name: "entry",
        component: () => import("@/views/entry.vue"),
        meta: {
            title: "入账申请",
            requireAuth: true,
            tabHidden: false,
        },
    },
    {
        path: "/outgoing",
        name: "outgoing",
        component: () => import("@/views/outgoing.vue"),
        meta: {
            title: "出账申请",
            requireAuth: true,
            tabHidden: false,
        },
    },
    {
        path: "/approvalList",
        name: "approvalList",
        component: () => import("@/views/approvalList.vue"),
        meta: {
            title: "我审批的",
            requireAuth: true,
            tabHidden: false,
            keepAlive: true,
        },
    },
    {
        path: "/submitList",
        name: "submitList",
        component: () => import("@/views/submitList.vue"),
        meta: {
            title: "我提交的",
            requireAuth: true,
            tabHidden: false,
            keepAlive: true,
        },
    },
    {
        path: "/detail",
        name: "detail",
        component: () => import("@/views/detail.vue"),
        meta: {
            title: "详情",
            requireAuth: true,
            tabHidden: true,
        },
    },
    {
        path: "/selectView",
        name: "selectView",
        component: () => import("@/views/selectView.vue"),
        meta: {
            title: "选择申请人",
            requireAuth: true,
            tabHidden: true,
        },
    },
    {
        path: "/menu_1",
        name: "menu_1",
        component: () => import("@/views/menu_1.vue"),
        meta: {
            title: "应收款",
            requireAuth: true,
            tabHidden: true,
        },
    },
    {
        path: "/yskmenu",
        name: "yskmenu",
        component: () => import("@/views/yskmenu.vue"),
        meta: {
            title: "应收款",
            requireAuth: true,
            tabHidden: true,
        },
    },
    {
        path: "/menu_2",
        name: "menu_2",
        component: () => import("@/views/menu_2.vue"),
        meta: {
            title: "回款率",
            requireAuth: true,
            tabHidden: true,
        },
    },
    {
        path: "/menu_3",
        name: "menu_3",
        component: () => import("@/views/menu_3.vue"),
        meta: {
            title: "预收款",
            requireAuth: true,
            tabHidden: true,
        },
    },
    {
        path: "/menu_4",
        name: "menu_4",
        component: () => import("@/views/menu_4.vue"),
        meta: {
            title: "业务量对比",
            requireAuth: true,
            tabHidden: true,
        },
    },
    {
        path: "/menu_5",
        name: "menu_5",
        component: () => import("@/views/menu_5.vue"),
        meta: {
            title: "客户含金量",
            requireAuth: true,
            tabHidden: true,
        },
    },
    {
        path: "/menu_6",
        name: "menu_6",
        component: () => import("@/views/menu_6.vue"),
        meta: {
            title: "账户详情",
            requireAuth: true,
            tabHidden: true,
        },
    },
    {
        path: "/IandEDetail",
        name: "IandEDetail",
        component: () => import("@/views/IandEDetail.vue"),
        meta: {
            title: "收支明细",
            requireAuth: true,
            tabHidden: true,
        },
    },
    {
        path: "/addAdvance",
        name: "addAdvance",
        component: () => import("@/views/addAdvance.vue"),
        meta: {
            title: "添加预收款",
            requireAuth: true,
            tabHidden: true,
        },
    },
    {
        path: "/collectionReview",
        name: "collectionReview",
        component: () => import("@/views/collectionReview.vue"),
        meta: {
            title: "待收款审核",
            requireAuth: true,
            tabHidden: true,
        },
    },
    {
        path: "/turnoverList",
        name: "turnoverList",
        component: () => import("@/views/turnoverList.vue"),
        meta: {
            title: "营业额列表",
            requireAuth: true,
            tabHidden: true,
        },
    },
    {
        path: "/visitlogs1",
        name: "visitlogs1",
        component: () => import("@/views/visitlogs1/index.vue"),
        meta: {
            title: "拜访日志",
            requireAuth: true,
            tabHidden: true,
        },
    },
    {
        path: "/visitlist",
        name: "visitlist",
        component: () => import("@/views/visitlist/index.vue"),
        meta: {
            title: "拜访查看",
            requireAuth: true,
            tabHidden: true,
        },
    },
    {
        path: "*",
        name: "error",
        component: () => import("@/views/error/index.vue"),
    },
];

const router = new VueRouter({
    // scrollBehavior: () => ({ y: 0 }),
    routes,
});

router.beforeEach((to, from, next) => {
    NProgress.start();
    let title = to.meta.title || to.name;
    document.title = title;
    let token = VueCookies.get("_user_finance");

    // 判断路由是否需要登录权限
    if (to.meta.requireAuth && !token) return next({ path: `/login` });

    //判断是否到index 页面
    if (to.path == "/index") {
        //如果有 清除sessionStorage中的visitlist_salesman
        if (sessionStorage.getItem("visitlist_salesman")) {
            sessionStorage.removeItem("visitlist_salesman");
        }
        //如果有 清除sessionStorage中的visitlist_outpatient
        if (sessionStorage.getItem("visitlist_outpatient")) {
            sessionStorage.removeItem("visitlist_outpatient");
        }
        //如果有 清除sessionStorage中的visitlist_time
        if (sessionStorage.getItem("visitlist_time")) {
            sessionStorage.removeItem("visitlist_time");
        }
    }

    next();
});

router.afterEach(() => {
    NProgress.done();
});

export default router;
